package org.mobile.model.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class DataUtil {

	public static Integer getIntegerValue(Map<String, Object> params,
			String key, Integer defaultValue) {
		try {
			return ((Number) params.get(key)).intValue();
		} catch (Exception e) {
		}
		return defaultValue;
	}

	public static Long getLongValue(Map<String, Object> params, String key,
			Long defaultValue) {
		try {
			return ((Number) params.get(key)).longValue();
		} catch (Exception e) {
		}
		return defaultValue;
	}

	public static Long getLongValue(Map<String, Object> params, String key) {
		return getLongValue(params, key, null);
	}

	public static Long getLongValue(HttpServletRequest request, String key,
			Long defaultValue) {
		try {
			String p = request.getParameter(key);
			return Long.parseLong(p);
		} catch (Exception e) {
		}
		return defaultValue;
	}
	
	public static Double getDoubleValue(HttpServletRequest request, String key,
			Double defaultValue) {
		try {
			String p = request.getParameter(key);
			return Double.parseDouble(p);
		} catch (Exception e) {
		}
		return defaultValue;
	}
	
	public static Integer getIntegerValue(HttpServletRequest request, String key,
			Integer defaultValue) {
		try {
			String p = request.getParameter(key);
			return Integer.parseInt(p);
		} catch (Exception e) {
		}
		return defaultValue;
	}
	
	public static Long getLongValue(HttpServletRequest request, String key) {
		return getLongValue(request, key, null);
	}
	
	public static Double getDoubleValue(HttpServletRequest request, String key) {
		return getDoubleValue(request, key, 0.0);
	}

	public static Integer getIntegerValue(HttpServletRequest request,
			String key) {
		return getIntegerValue(request, key, null);
	}
	

}
